//
//  MYMyNotificationVC.m
//  MuYing
//
//  Created by tbb on 2019/8/6.
//  Copyright © 2019 TBB. All rights reserved.
// notification_header_icon

#import "MYMyNotificationVC.h"
#import "MYMyNotificationCell.h"
#import "MYMyNotificationModel.h"
@interface MYMyNotificationVC ()
@property (nonatomic, assign) BOOL isAllRead;//标记为全部已读
@end

@implementation MYMyNotificationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.isAllRead = NO;
    self.navigationItem.title = kYQLS(@"通知");
    self.rightItemTitle = kYQLS(@"全部已读");
}

-(void)rightItemAction:(UIButton *)sender {
    if (!self.isAllRead) {
        self.isAllRead = YES;
    }
    
    if (self.dataArr.count == 0) {
        return;
    }
    
    NSMutableArray *arr = @[].mutableCopy;
    for (MYMyNotificationModel *notiModel in self.dataArr) {
        if (!notiModel.state) {
            [arr addObject:notiModel.messageId];
        }
    }
    if (arr.count > 0) {
        [self setNotiReadArr:arr.copy];
    }
    
}

-(void)setupView {
    [super setupView];
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMyNotificationCell")
    [self setupRefresh:self.listTableView];
    [self.listTableView.mj_header beginRefreshing];
}

-(void)onStartRefresh {
    self.page = 1;
    [self getNotificationList];
}

-(void)onStartLoadMore {
    self.page ++ ;
    [self getNotificationList];
}

-(void)getNotificationList {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"limit"] = @"10";
    parameters[@"page"] = @(self.page);
    [YQPNetWorkingManager sendPostRequest:kPOST_QUERY_MESSAGE_LIST_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            NSArray *arr = [NSArray modelArrayWithClass:MYMyNotificationModel.class json:responseObject.data[@"list"]];
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
            if (arr.count < 10) {
                [self.listTableView.mj_footer endRefreshingWithNoMoreData];
            }
            
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self stopRefresh];
        YQUITIPSERROR(error)
    }];
}

-(void)setNotiReadArr:(NSArray *)arr {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"messageId"] = [arr componentsJoinedByString:@","];
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_MODIFY_MESSAGE_READSTATUS_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            YQUITIPSSUCCEED(responseObject.msg)
            [self getNotificationList];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYMyNotificationModel *notModle = nil;
    if (indexPath.row < self.dataArr.count) {
        notModle = self.dataArr[indexPath.row];
    }
    MYMyNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMyNotificationCell"];
    cell.notModel = notModle;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYMyNotificationModel *notModle = nil;
    if (indexPath.row < self.dataArr.count) {
        notModle = self.dataArr[indexPath.row];
    }
    
    return [tableView fd_heightForCellWithIdentifier:@"MYMyNotificationCell" configuration:^(MYMyNotificationCell *cell) {
        cell.notModel = notModle;
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MYMyNotificationModel *notModle = nil;
    if (indexPath.row < self.dataArr.count) {
        notModle = self.dataArr[indexPath.row];
    }
    if (!notModle.isState) {
        [self setNotiReadArr: @[notModle.messageId]];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
